package com.android.settings.audio.peq.peq.filter;

import static java.lang.Math.cos;
import static java.lang.Math.pow;
import static java.lang.Math.sin;

import com.android.settings.audio.peq.peq.Coeff;

/**
 * Created by We on 2023/3/18
 */
public class FilterPeaking implements IFilter {
   /*
    * function [coeff_b,coeff_a] = GetPeakEQ(f,Q,dBgain,fs)
        %%% peak EQ coeff
        w0 = 2*pi*f/fs;
        alpha = sin(w0)/(2*Q);
        A  =   10^(dBgain/40);
        b0 =   1 + alpha*A;
        b1 =  -2*cos(w0);
        b2 =   1 - alpha*A;
        a0 =   1 + alpha/A;
        a1 =  -2*cos(w0);
        a2 =   1 - alpha/A;
        %%%
        coeff_b=[b0,b1,b2]';
        coeff_a=[a0,a1,a2]';
    end
    * */

    @Override
    public Coeff getValue(double f, double Q, double dBGain, int fs) {
        double w0 = 2 * Math.PI * f / fs;
        double alpha = sin(w0) / (2 * Q);
        double A = pow(10, dBGain / 40);

        double b0 = 1 + alpha * A;
        double b1 = -2 * cos(w0);
        double b2 = 1 - alpha * A;
        double a0 = 1 + alpha / A;
        double a1 = -2 * cos(w0);
        double a2 = 1 - alpha / A;
        Coeff coeff = new Coeff();
        coeff.setB0(b0);
        coeff.setB1(b1);
        coeff.setB2(b2);
        coeff.setA0(a0);
        coeff.setA1(a1);
        coeff.setA2(a2);
        return coeff;
    }
}
